﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using BLL;
using Entities;
namespace Stationery.UC
{
    public partial class UC_ReportBasket : System.Web.UI.UserControl
    {
        BLL_Order bll_order = new BLL_Order();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["id"] != null)
            {
                string strID = Request.Params["id"].ToString();
                switch (strID)
                {
                    case "neworder":
                        Get_OrderList_Grid("P");
                        break;
                    case "successfulorder":
                        Get_OrderList_Grid("S");
                        ASPxGridView1.Columns["function"].Visible = false;
                        break;
                    case "cancelorder":
                        Get_OrderList_Grid("D");
                        ASPxGridView1.Columns["function"].Visible = false;
                        break;
                    default:
                        break;
                }
            }
        }
        private void Get_OrderList_Grid(string pStatus)
        {
            ASPxGridView1.DataSource = bll_order.Get_OrderList(pStatus);
            ASPxGridView1.DataBind();
        }

        protected void LinkButtonSuccess_Click(object sender, EventArgs e)
        {
            string strOrder_Code = ((LinkButton)sender).CommandArgument.ToString();
            List<Order> lstOrder = bll_order.Lst_Order(bll_order.Get_Order_OrderCode(strOrder_Code));
            foreach (Order order in lstOrder)
            {
                order.STATUS = Order.status_success;
                bll_order.InsertUpdate_Order(order);
            }
            Response.Redirect(Request.RawUrl);
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            string strOrder_Code = ((LinkButton)sender).CommandArgument.ToString();
            List<Order> lstOrder = bll_order.Lst_Order(bll_order.Get_Order_OrderCode(strOrder_Code));
            foreach (Order order in lstOrder)
            {
                order.STATUS = Order.status_delete;
                bll_order.InsertUpdate_Order(order);
            }
            Response.Redirect(Request.RawUrl);
        }

        protected void LinkButtonDetail_Click(object sender, EventArgs e)
        {
            string strOrder_Code = ((LinkButton)sender).CommandArgument.ToString();
            List<Order> lstOrder = bll_order.Lst_Order(bll_order.Get_Order_OrderCode(strOrder_Code));
            for (int i = 0; i < lstOrder.Count; i++)
            {
                DataTable dt = (new BLL_Product()).Get_Product(lstOrder[i].ID_PRODUCT);
                if (dt.Rows.Count > 0)
                {
                    lstOrder[i].PRODUCT_DETAIL = (new BLL_Product()).Lst_Product(dt)[0];
                }
            }
            Session[Utils.SESSION_CART_ADMIN] = lstOrder;
            Response.Redirect("~/Admin.aspx?id=orderdetail&order_code=" + strOrder_Code);
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.WriteXlsToResponse(true);
        }

        protected void LinkButtonExcel_Click(object sender, EventArgs e)
        {
            if (Request.Params["id"] != null)
            {
                string strID = Request.Params["id"].ToString();
                string strOrderCode = ((LinkButton)sender).CommandArgument.ToString(); ;
                string strFileName = Server.MapPath(Utils.excel_directory_temp + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".xls");
                DataTable dt = bll_order.Get_Order_OrderCode(strOrderCode);
                //                
                List<Order> lstOrder = bll_order.Lst_Order(dt);
                MemoryStream ms = bll_order.ExportToExcel_NPOI(Server.MapPath(Utils.excel_directory), lstOrder, strFileName);
                string saveAsFileName = string.Format("HoaDon-{0:d}-{1}-{2}.xls", lstOrder[0].RECEIVE_DATE.ToString("ddMMyyyy"),lstOrder[0].FULLNAME.Trim(),lstOrder[0].ADDRESS.Trim());

                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
                Response.Clear();
                Response.BinaryWrite(ms.ToArray());
                Response.End();
            }
        }
    }
}